//
//  MKRetweetedView.swift
//  Weibo
//
//  Created by mac on 15/12/20.
//  Copyright © 2015年 MK. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

class MKRetweetedView: UIView {
    
    //1.声明一个约束对象属性  2.给约束对象属性赋值 3.干掉约束 4.重设置约束
    var constraintB: Constraint?
    
    //属性
    var retweetStatus: MKStatusModel? {
        didSet {
            retweetLabel.text = "@" + (retweetStatus?.user?.name ?? "") + (retweetStatus?.text ?? "")
            retweetImage.urlArr = retweetStatus?.pic_nsurls
            
            constraintB!.uninstall()
            
            if let urls = retweetStatus?.pic_nsurls where urls.count > 0 {
                self.snp_updateConstraints(closure: { (make) -> Void in
                   self.constraintB = make.bottom.equalTo(retweetImage.snp_bottom).offset(cellIconMargin).constraint
                })
                
            } else {
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.constraintB = make.bottom.equalTo(retweetLabel.snp_bottom).offset(cellIconMargin).constraint
                })
                
            }
            
        }
        
        
    }
    
    //入口
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpViews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置子视图
    func setUpViews() {
        addSubview(retweetLabel)
        addSubview(retweetImage)
        
        retweetLabel.snp_makeConstraints { (make) -> Void in
            make.top.left.equalTo(self).offset(cellIconMargin)
        }
        
        retweetImage.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(retweetLabel.snp_bottom).offset(cellIconMargin)
            make.left.equalTo(retweetLabel)
        }
        
        //自动计算行高
        self.snp_makeConstraints { (make) -> Void in
            self.constraintB = make.bottom.equalTo(retweetImage.snp_bottom).offset(cellIconMargin).constraint
        }
        
    }
    
    //懒加载子视图
    private var retweetLabel: UILabel = UILabel(tText: "转发微博", tColor: UIColor.darkGrayColor(), tFont: 12, isNum: 12)
    private var retweetImage: MKPictureView = MKPictureView()
    
}
